import axios from 'axios';
import store from '@/utils/store.js';
import { Message } from 'element-ui';


//做个性化定制， 
const service  = axios.create({
  baseURL: 'http://localhost:8080',
  timeout: 10000,
  // headers: {'X-Custom-Header': 'foobar'}
});

service.interceptors.request.use(config=>{
 //请求的时候携带token
  const token = store.state.token;
  if(token){
    // alert("携带token: is  "+token)
    config.headers['Authentication'] = token
  }
  return config;
})

service.interceptors.response.use(response=> {
  const res = response.data;
  const {code, data, msg} = res;
  if (code == 200) {
    return data;
  }else if(code == 401 || code ==404 || code==500){
    Message.error(msg ||  "接口异常....");
    return Promise.reject(msg ||  "接口异常....")
  }

  return null
})


export default service